import datetime
import logging
from mgmt.models import Batch, BatchCar
from student.models import StudentInvite, StudentInvitePerson
from teacher.models import TeacherInvite, TeacherInviteCar, TeacherInvitePerson
from visitor.models import VisitorApply, VisitorApplyCar, VisitorApplyPerson
from ymiot import views as ymiotapi

logger = logging.getLogger("log")


# 教师邀请工单人员入场状态同步
def teacher_personenter_sync():
    logger.info("===开始===教师邀请工单人员入场状态同步===")
    # 教师邀请
    teacher_invite_list = TeacherInvite.objects.filter(status=5)
    for teacher_invite in teacher_invite_list:
        if TeacherInviteCar.objects.filter(invite=teacher_invite).count() > 0:
            continue
        logger.info(f"===工单==={teacher_invite.__str__()}===")
        teacher_invite_person_list = TeacherInvitePerson.objects.filter(
            invite=teacher_invite
        )
        enter_flag = 1
        for teacher_invite_person in teacher_invite_person_list:
            person = teacher_invite_person.person
            if person.status != 3:
                enter_flag = 0
        if enter_flag == 1:
            teacher_invite.status = 7
            teacher_invite.save()
        else:
            continue
    logger.info("===结束===教师邀请工单人员入场状态同步===")


# 教师邀请工单人员出场状态同步
def teacher_personout_sync():
    logger.info("===开始===教师邀请工单人员出场状态同步===")
    # 教师邀请
    teacher_invite_list = TeacherInvite.objects.filter(status=7)
    for teacher_invite in teacher_invite_list:
        if TeacherInviteCar.objects.filter(invite=teacher_invite).count() > 0:
            continue
        logger.info(f"===工单==={teacher_invite.__str__()}===")
        teacher_invite_person_list = TeacherInvitePerson.objects.filter(
            invite=teacher_invite
        )
        enter_flag = 1
        for teacher_invite_person in teacher_invite_person_list:
            person = teacher_invite_person.person
            if person.status != 4:
                enter_flag = 0
        if enter_flag == 1:
            teacher_invite.status = 8
            teacher_invite.save()
        else:
            continue
    logger.info("===结束===教师邀请工单人员出场状态同步===")


# 访客申请工单人员入场状态同步
def visitor_personenter_sync():
    logger.info("===开始===访客申请工单人员入场状态同步===")
    # 访客申请
    visitor_apply_list = VisitorApply.objects.filter(status=5)
    for visitor_apply in visitor_apply_list:
        if VisitorApplyCar.objects.filter(apply=visitor_apply).count() > 0:
            continue
        apply_person_list = VisitorApplyPerson.objects.filter(apply=visitor_apply)
        enter_flag = 1
        for apply_person in apply_person_list:
            person = apply_person.person
            if person.status != 3:
                enter_flag = 0
        if enter_flag == 1:
            visitor_apply.status = 7
            visitor_apply.save()
        else:
            continue
    logger.info("===结束===访客申请工单人员入场状态同步===")


# 访客申请工单人员出场状态同步
def visitor_personout_sync():
    logger.info("===开始===访客申请工单人员出场状态同步===")
    # 访客申请
    visitor_apply_list = VisitorApply.objects.filter(status=7)
    for visitor_apply in visitor_apply_list:
        if VisitorApplyCar.objects.filter(apply=visitor_apply).count() > 0:
            continue
        apply_person_list = VisitorApplyPerson.objects.filter(apply=visitor_apply)
        enter_flag = 1
        for apply_person in apply_person_list:
            person = apply_person.person
            if person.status != 4:
                enter_flag = 0
        if enter_flag == 1:
            visitor_apply.status = 8
            visitor_apply.save()
        else:
            continue
    logger.info("===结束===访客申请工单人员出场状态同步===")


# 学生邀请工单人员入场状态同步
def teacher_personenter_sync():
    logger.info("===开始===学生邀请工单人员入场状态同步===")
    # 教师邀请
    student_invite_list = StudentInvite.objects.filter(status=5)
    for student_invite in student_invite_list:
        logger.info(f"===工单==={student_invite.__str__()}===")
        student_invite_person_list = StudentInvitePerson.objects.filter(
            invite=student_invite
        )
        enter_flag = 1
        for student_invite_person in student_invite_person_list:
            person = student_invite_person.person
            if person.status != 3:
                enter_flag = 0
        if enter_flag == 1:
            student_invite.status = 7
            student_invite.save()
        else:
            continue
    logger.info("===结束===学生邀请工单人员入场状态同步===")


# 学生邀请工单人员出场状态同步
def teacher_personout_sync():
    logger.info("===开始===学生邀请工单人员出场状态同步===")
    student_invite_list = StudentInvite.objects.filter(status=7)
    for student_invite in student_invite_list:
        logger.info(f"===工单==={student_invite.__str__()}===")
        student_invite_person_list = StudentInvitePerson.objects.filter(
            invite=student_invite
        )
        out_flag = 1
        for student_invite_person in student_invite_person_list:
            person = student_invite_person.person
            if person.status != 4:
                out_flag = 0
        if out_flag == 1:
            student_invite.status = 8
            student_invite.save()
        else:
            continue
    logger.info("===结束===学生邀请工单人员出场状态同步===")
